﻿using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;
using System.Windows.Data;
using JY.Wpf.Core.Tools;
using JY.Wpf.Core.ViewModelBases;

namespace JY.Wpf.TestForMetro.ViewModels.ControlDemoViewModels
{
    [NotifyPropertyChanged]
    public class InputDemoViewModel : ViewModelBase
    {
        #region # 变量

        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public InputDemoViewModel()
        {
            var converter = new StringRepeatConverter();
            this.DataList=new ObservableCollection<string>();
            for (var i = 1; i <= 9; i++)
            {
                this.DataList.Add($"{converter.Convert("正文", null, i, CultureInfo.CurrentCulture)}{i}");
            }
        }
        #endregion

        #region # 属性

        public ObservableCollection<string> DataList { get; set; }

        #endregion

        #region # Command方法
        #endregion

        #region # 方法

        #endregion

        #region # 获取页面 —— object GetView(object context = null)
        /// <summary>
        /// 获取页面
        /// </summary>
        /// <param name="context">页面</param>
        /// <returns></returns>
        public override object GetView(object context = null)
        {
            return ResolveMediator.Resolve<InputDemoView>();
        }
        #endregion
    }
    public class StringRepeatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string strValue)
            {
                var builder = new StringBuilder();
                int num;
                if (parameter is string numStr)
                {
                    if (!int.TryParse(numStr, out num))
                    {
                        return strValue;
                    }
                }
                else if (parameter is int intValue)
                {
                    num = intValue;
                }
                else
                {
                    return strValue;
                }
                for (var i = 0; i < num; i++)
                {
                    builder.Append(strValue);
                }
                return builder.ToString();
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
